﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionAutoEcoleModel;
using log4net;
using GestionAutoEcole.DAO.Util;
using System.Data.Objects;

namespace GestionAutoEcole.DAO.Impl
{
    public class ExamenDaoImpl : BaseRepository<examen>
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ExamenDaoImpl));

        private static volatile ExamenDaoImpl instance;
        private static object syncRoot = new Object();

        private ExamenDaoImpl() { }

        public static ExamenDaoImpl getInstance()
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new ExamenDaoImpl();
                }
            }

            return instance;
        }
        //création d'un groupe
        public override void Create(examen el)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin Create");
                }
                using (var context = new autoecolebase())
                {
                    // il faut plutot utiliser ça : context.VUGroupe.AddObject(grp);
                    context.examen.AddObject(el);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("Create : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End Create");
                }
            }
        }

        //modification
        public override void Update(examen entity)
        {

        }


        //Supprimer tous les groupe
        public override void DeleteAll()
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin DeleteAll");
                }
                using (var context = new autoecolebase())
                {
                    var examenList = context.examen;
                    foreach (var el in examenList)
                    {
                        context.DeleteObject(el);
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("DeleteAll : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End DeleteAll");
                }
            }
        }

        //Supprimer un groupe
        public override void Delete(examen el)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin Delete");
                }
                using (var context = new autoecolebase())
                {
                    var pers = context.examen.Where(u => u.EXAMEN_ID == el.EXAMEN_ID).First();
                    context.DeleteObject(pers);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("Delete : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End Delete");
                }
            }
        }

        //Récupérer tous les groupes
        public override List<examen> GetList()
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin GetList");
                }
                using (var context = new autoecolebase())
                {
                    return context.examen.ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("GetList : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End GetList");
                }
            }
        }

        //Récupérer un groupe avec son Id
        public override examen FindById(int id)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin FindById");
                }
                using (var context = new autoecolebase())
                {
                    var grp = context.examen.Where(u => u.EXAMEN_ID == id);
                    return grp.First();
                }
            }
            catch (Exception ex)
            {
                log.Error("FindById : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End FindById");
                }
            }
        }

        //Récupérer une liste d'groupes avec un critère de recherche
        public override List<examen> FindByCriteria(Object criteria)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin FindByCriteria");
                }
                using (var context = new autoecolebase())
                {

                    StringBuilder queryString =
                        new StringBuilder(@"SELECT VALUE grp FROM autoecolebase.examen as grp");
                    SelectBuilder sb = new SelectBuilder();

                    examen grp = (examen)criteria;
                    if (grp.EXAMEN_LABEL != null && !grp.EXAMEN_LABEL.Equals(""))
                    {
                        sb.AndSearchLike("grp.EXAMEN_LABEL", grp.EXAMEN_LABEL);
                    }

                    if (grp.DATE_EXAMEN != null)
                    {
                        sb.AndSearchLike("grp.DATE_EXAMEN", grp.DATE_EXAMEN.ToString());
                    }

                    if (grp.COURS_ID != 0)
                    {
                        sb.AndSearchLike("grp.COURS_ID", grp.COURS_ID.ToString());
                    }

                    if (grp.EXAMINATEUR_ID != 0)
                    {
                        sb.AndSearchLike("grp.EXAMINATEUR_ID", grp.EXAMINATEUR_ID.ToString());
                    }

                    if (grp.EXAMINATEUR_ID != 0)
                    {
                        sb.AndSearchLike("grp.EXAMINATEUR_ID", grp.EXAMINATEUR_ID.ToString());
                    }

                    if (grp.PERMIS_ID != 0)
                    {
                        sb.AndSearchLike("grp.PERMIS_ID", grp.PERMIS_ID.ToString());
                    }

                    queryString.Append(sb.getQueryString());
                    ObjectQuery<examen> query = new ObjectQuery<examen>(queryString.ToString(), context);
                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("FindByCriteria : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End FindByCriteria");
                }
            }
        }

    }
}
